package com.scnu.yxp.travelapp.newedit;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.scnu.yxp.travelapp.R;
import com.scnu.yxp.travelapp.tool.httpUtil;

public class EditDairy extends Activity {
	private ImageView backBtn;
	//飞机图像
	private ImageView plane;
	//帧动画
	private AnimationDrawable anim;
	private EditText content;
	private Button okBtn;
	private String response;
	private String neirong = "";
	private String biaoti = "";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_edit);
		backBtn = (ImageView)findViewById(R.id.back_btn);
		content = (EditText)findViewById(R.id.content);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		plane = (ImageView)findViewById(R.id.plane);
		okBtn = (Button)findViewById(R.id.ok);
		okBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(!content.getText().toString().equals(""))
				{
					//帧动画设置
			        plane.setBackgroundResource(R.drawable.edit_plane);
			        anim = (AnimationDrawable)plane.getBackground();
					anim.start();
					try {
						neirong = URLEncoder.encode(content.getText().toString(), "UTF-8");
						biaoti = URLEncoder.encode("paris", "UTF-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					Thread thread = new Thread(new Runnable() {
						
						@Override
						public void run() {
							response = httpUtil.sendPost("http://2.travelaround.sinaapp.com/handlexieru",
									"user.biaoti=" + biaoti + "&user.neirong=" + neirong);
							handler.sendEmptyMessage(0x120);
						}
					});
					thread.start();
				}else{
					Toast.makeText(EditDairy.this, "内容为空，无法发送", Toast.LENGTH_SHORT).show();
				}
		        
			}
		});
		
		Intent intent = getIntent();
		String load = intent.getStringExtra("load");
		if(load != null && load.equals("load"))
		{
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						biaoti = URLEncoder.encode("paris", "UTF-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					response = httpUtil.sendPost("http://2.travelaround.sinaapp.com/handleduqu",
							"user.biaoti=" + biaoti);
					Log.i("dd",response);
					handler.sendEmptyMessage(0x121);
				}
			});
			thread.start();
		}
	}
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 0x120)
			{
				if(response.contains("right"))
				{
					Toast.makeText(EditDairy.this, "保存成功", Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(EditDairy.this, "保存失败", Toast.LENGTH_SHORT).show();
					anim.stop();
				}
			}
			if(msg.what == 0x121)
			{
				if(!response.contains("wu"))
				{
					content.setText(response);
				}
				
			}
		}};
}
